import socket
import requests
import tkinter.messagebox as msgbox
import re
import random
import math


# 获取本机IP地址
def get_localhost():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    return s.getsockname()[0]


# 获取参数列表
def get_params():
    return {
        'c': 'Portal',
        'a': 'logout',
        'callback': 'dr1004',
        'login_method': '1',
        'user_account': 'drcom',
        'user_password': '123',
        'ac_logout': '0',
        'register_mode': '1',
        'wlan_user_ip': get_localhost(),
        'wlan_user_ipv6': '',
        'wlan_vlan_id': '0',
        'wlan_user_mac': '000000000000',
        'wlan_ac_ip': '172.16.253.1',
        'wlan_ac_name': '',
        'jsVersion': '3.3.2',
        'v': str(math.floor(random.random() * 10000 + 500))
    }


if __name__ == '__main__':
    #
    url = 'http://172.16.253.3:801/eportal/'
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.62 Safari/537.36'}
    params = get_params()
    print(params)
    htmls = requests.get(url=url, params=params, headers=headers)
    print("请求url:%s" % htmls.request.url)
    print("请求头:%s" % htmls.request.headers)
    print("响应头:%s" % htmls.headers)
    print(htmls.text)
    str3 = re.findall('"(.*?)"', htmls.text)
    if str3[1] == '1':
        msgbox.showinfo('成功', '退出成功')
    else:
        msgbox.showinfo('失败', '退出失败')
